﻿const config = require("../config")
const rsa = require("../utils/rsa")
const jwt = require('jsonwebtoken');
/**
 * 拦截器，验证 token，data
 * @type {{Validator(*, *, *): void}}
 */
module.exports = {
    Validator(req, res, next) {
        if(req.url == config.filter.login || req.url == config.filter.add) {
            next()
        } else {
            if(req.headers.hasOwnProperty("authorizations")) {
                jwt.verify(req.headers.authorizations,config.jwt,function(err,decode){
                    if(err){
                        res.json({
                            status: 500,
                            message: '身份凭证失效或不存在，请登录'
                        })
                    }else{
                        next();
                    }
                });
            } else { // 无token
                res.json({
                    status: 500,
                    message: '缺少token参数，请检查',
                })
            }
        }
    }
}
